#!/usr/bin/env python
# -*- coding: utf-8 -*-

from rest_framework import serializers

from hony.models import (AuthorModel, ArticleModels)

class AuthorSerializer(serializers.ModelSerializer):
    class Meta:
        model = AuthorModel
        fields = '__all__'

class ArticleListSerializer(serializers.ListSerializer):
    def to_representation(self, data):
        res = []
        for d in data:
            res.append('{id}  {title}'.format(id=d.id, title=d.title))
        return res

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = ArticleModels
        fields = '__all__'
        list_serializer_class = ArticleListSerializer

    def to_representation(self, instance):
        return {'res':'{title}\n{author}\n{content}'.format(title=instance.title, author=instance.author.name, content=instance.content)}


class ArticleWxXCXListSerilizer(serializers.ListSerializer):
    def to_representation(self, data):
        data = super().to_representation(data)
        for d in data:
            d.pop("content")
        return data


class ArticleWxXCXSerilizer(serializers.ModelSerializer):
    content = serializers.SerializerMethodField()

    class Meta:
        model = ArticleModels
        fields = '__all__'
        list_serializer_class = ArticleWxXCXListSerilizer
        depth = 2

    def get_content(self, obj, *args, **kwargs):
        return obj.get_content()
